package com.atguigu.gmall.list;

import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.sort.ScoreSortBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import javax.ws.rs.GET;
import java.util.Map;

/**
 * @Author: lx
 * @Time: 14:54
 * @Description:
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class ESDemo {

    //@Autowired
    //private ElasticsearchRestTemplate elasticsearchRestTemplate;//Spring Data ElasticSearch

    @Autowired
    private RestHighLevelClient restHighLevelClient;
    //开始搜索
    @Test
    public void esTest() throws Exception{
        //1:连接ES索引库
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("goods");
        //searchRequest.types("info","haha","hehe");
        //构建条件对象
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //查询所有
 /*       GET goods/_search
        {
            "query": {
            "match_all": {}
        }
        }*/
        //QueryBuilders.matchAllQuery()
        //查询所有
        //QueryBuilder queryBuilder = new MatchAllQueryBuilder();
        //1、关键词  查看所有
        //searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        searchSourceBuilder.query(QueryBuilders.matchQuery("title","手机"));// select * from 表 where name = 精准 like
        //2：分页  （当前页-1）* 每页数
        int pageNo = 1;
        int pageSize = 5;
        searchSourceBuilder.from((pageNo-1)*pageSize); //开始行
        searchSourceBuilder.size(pageSize);//每页数
        //3:排序
        //searchSourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC));
        searchSourceBuilder.sort("hotScore",SortOrder.DESC);
        //4:高亮  Redmi 9A <font sytle='color:red'>手机</font>
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder
                 .field("title")
                 .preTags("<font style='color:red'>")
                 .postTags("</font>");
        searchSourceBuilder.highlighter(highlightBuilder);
        searchRequest.source(searchSourceBuilder);

        System.out.println(searchSourceBuilder.toString());

        //参数1：搜索请求对象
        //参数2：搜索选项 默认
        SearchResponse searchResponse =
                restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        //解析结果
        SearchHits hits = searchResponse.getHits();
        //总条数
        System.out.println("总条数：" +  hits.totalHits);
        //结果集
        SearchHit[] hits1 = hits.getHits();

        for (SearchHit hit : hits1) {
            System.out.println(hit.getSourceAsString());//json字符串 转成Goods

            Map<String, HighlightField> highlightFields = hit.getHighlightFields();
            HighlightField highlightField = highlightFields.get("title");
            System.out.println(highlightField.fragments()[0].toString());

        }


    }
}
